home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) '''Abstract user interface, which provides all logic and strings. Concrete implementations need to implement a set of abstract presentation functions with an appropriate toolkit. ''' import gettext import optparse import urllib2 import tempfile import sys import time import os import threading import gobject import dbus import dbus.service as dbus import dbus.mainloop.glib as dbus from oslib import OSLib from backend import UnknownHandlerException, PermissionDeniedByPolicy, BackendCrashError, polkit_auth_wrapper, dbus_sync_call_signal_wrapper, Backend, DBUS_BUS_NAME def bool(str): '''Convert backend encoding of a boolean to a real boolean.''' if str == 'True': return True if not str == 'False': raise AssertionError return False def __fix_stdouterr(): import codecs import locale def null_decode(input, errors = 'strict'): return (input, len(input)) sys.stdout = codecs.EncodedFile(sys.stdout, locale.getpreferredencoding()) sys.stdout.decode = null_decode sys.stderr = codecs.EncodedFile(sys.stderr, locale.getpreferredencoding()) sys.stderr.decode = null_decode __fix_stdouterr() class AbstractUI(dbus.service.Object): '''Abstract user interface. This encapsulates the entire program logic and all strings, but does not implement any concrete user interface. ''' def __init__(self): '''Initialize system. This parses command line arguments, detects available hardware, and already installed drivers and handlers. ''' gettext.install('jockey', unicode = True) (self.argv_options, self.argv_args) = self.parse_argv() if self.argv_options.check: time.sleep(self.argv_options.check) self.init_strings() self._dbus_iface = None self.dbus_server_main_loop = None self.have_ui = False self.current_search = (None, None) def backend(self): '''Return D-BUS backend client interface. This gets initialized lazily. ''' if self._dbus_iface is None: try: self._dbus_iface = Backend.create_dbus_client() except Exception: e = None if hasattr(e, '_dbus_error_name') and e._dbus_error_name == 'org.freedesktop.DBus.Error.FileNotFound': if self.have_ui: self.error_message(self._('Cannot connect to D-BUS'), str(e)) else: self.error_msg(str(e)) sys.exit(1) else: raise e._dbus_error_name == 'org.freedesktop.DBus.Error.FileNotFound' self._call_progress_dialog(self._('Searching for available drivers...'), self._dbus_iface.detect, timeout = 600) else: try: self._dbus_iface.handler_info(' ') except Exception: e = None if hasattr(e, '_dbus_error_name') and e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown': self._dbus_iface = Backend.create_dbus_client() self._call_progress_dialog(self._('Searching for available drivers...'), self._dbus_iface.detect, timeout = 600) except: e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown' return self._dbus_iface def _(self, str, convert_keybindings = False): """Keyboard accelerator aware gettext() wrapper. This optionally converts keyboard accelerators to the appropriate format for the frontend. All strings in the source code should use the '_' prefix for key accelerators (like in GTK). For inserting a real '_', use '__'. """ result = _(str) if convert_keybindings: result = self.convert_keybindings(result) return result def init_strings(self): '''Initialize all static strings which are used in UI implementations.''' self.string_handler = self._('Component') self.string_button_enable = self._('_Enable', True) self.string_button_disable = self._('_Disable', True) self.string_enabled = self._('Enabled') self.string_disabled = self._('Disabled') self.string_status = self._('Status') self.string_restart = self._('Needs computer restart') self.string_in_use = self._('In use') self.string_not_in_use = self._('Not in use') self.string_license_label = self._('License:') self.string_details = self._('details') self.string_free = self._('Free') self.string_restricted = self._('Proprietary') self.string_download_progress_title = self._('Download in progress') self.string_unknown_driver = self._('Unknown driver') self.string_unprivileged = self._('You are not authorized to perform this action.') self.string_support_certified = self._('Tested by the %s developers') % OSLib.inst.os_vendor self.string_support_uncertified = self._('Not tested by the %s developers') % OSLib.inst.os_vendor self.string_recommended = self._('Recommended') self.string_license_dialog_title = self._('License Text for Device Driver') def main_window_title(self): '''Return an appropriate translated window title. This might depend on the mode the program is called (e. g. showing only free drivers, only restricted ones, etc.). ''' if self.argv_options.mode == 'nonfree': return self._('Restricted Hardware Drivers') return self._('Hardware Drivers') def main_window_text(self): '''Return a tuple (heading, subtext) of main window texts. This changes depending on whether restricted or free drivers are used/available, or if a search is currently running. Thus the UI should update it whenever it changes a handler. ''' if self.current_search[0]: return (self._('Driver search results'), self.hwid_to_display_string(self.current_search[0])) proprietary_in_use = False proprietary_available = False for h_id in self.backend().available(self.argv_options.mode): info = self.backend().handler_info(h_id) if not bool(info['free']): proprietary_available = True if bool(info['used']): proprietary_in_use = True break bool(info['used']) if proprietary_in_use: heading = self._('Proprietary drivers are being used to make this computer work properly.') else: heading = self._('No proprietary drivers are in use on this system.') if proprietary_available: subtext = self._('Proprietary drivers do not have public source code that %(os)s developers are free to modify. They represent a risk to you because they are only available on the types of computer chosen by the manufacturer, and security updates to them depend solely on the responsiveness of the manufacturer. %(os)s cannot fix or improve these drivers.') % { 'os': OSLib.inst.os_vendor } else: subtext = '' return (heading, subtext) def get_handler_category(self, handler_id): '''Return string for handler category.''' if handler_id.startswith('xorg:'): return self._('Graphics driver') if handler_id.startswith('firmware:'): return self._('Firmware') return self._('Device driver') def get_ui_driver_name(self, handler_info): '''Return handler name, as it should be presented in the UI. This cares about translation, as well as tagging recommended drivers. ''' result = handler_info['name'] result = self._(result) if 'version' in handler_info: result += ' (%s)' % self._('version %s') % handler_info['version'] if bool(handler_info['recommended']): result += ' [%s]' % self.string_recommended return result def get_ui_driver_info(self, handler_id): '''Get strings and icon types for displaying driver information. If handler_id is None, this returns empty strings, suitable for displaying if no driver is selected, and "None" for the bool values, (UIs should disable the corresponding UI element then). This returns a mapping with the following keys: name (string), description (string), certified (bool, for icon), certification_label (label string), free (bool, for icon), license_label (Free/Proprietary, label string), license_text (string, might be empty), enabled (bool, for icon), used (bool), needs_reboot(bool), status_label (label string), button_toggle_label (string)''' if not handler_id: return { 'name': '', 'description': '', 'free': None, 'enabled': None, 'used': None, 'license_text': '', 'status_label': '', 'license_label': '', 'certified': None, 'certification_label': '', 'button_toggle_label': None } info = polkit_auth_wrapper(self.backend().handler_info, handler_id) result = { 'name': self.get_ui_driver_name(info), 'description': self._get_description_rationale_text(info), 'free': bool(info['free']), 'enabled': bool(info['enabled']), 'used': bool(info['used']), 'needs_reboot': False, 'license_text': info.get('license', '') } if result['free']: result['license_label'] = self.string_free else: result['license_label'] = self.string_restricted if 'repository' not in info: result['certified'] = True result['certification_label'] = self.string_support_certified else: result['certified'] = False result['certification_label'] = self.string_support_uncertified if result['enabled']: if 'package' in info: result['button_toggle_label'] = self._('_Remove', True) else: result['button_toggle_label'] = self._('_Deactivate', True) if result['used']: result['status_label'] = self._('This driver is activated and currently in use.') elif bool(info['changed']): result['needs_reboot'] = True result['status_label'] = self._('You need to restart the computer to activate this driver.') else: result['status_label'] = self._('This driver is activated but not currently in use.') else: result['button_toggle_label'] = self._('_Activate', True) if result['used']: if bool(info['changed']): result['needs_reboot'] = True result['status_label'] = self._('This driver was just disabled, but is still in use.') else: result['status_label'] = self._('A different version of this driver is in use.') else: result['status_label'] = self._('This driver is not activated.') return result def parse_argv(self): '''Parse command line arguments, and return (options, args) pair.''' def check_option_callback(option, opt_str, value, parser): if len(parser.rargs) > 0 and parser.rargs[0].isdigit(): setattr(parser.values, 'check', int(parser.rargs.pop(0))) else: setattr(parser.values, 'check', 0) parser = optparse.OptionParser() parser.set_defaults(check = None) parser.add_option('-c', '--check', action = 'callback', callback = check_option_callback, help = self._('Check for newly used or usable drivers and notify the user.')) parser.add_option('-u', '--update-db', action = 'store_true', dest = 'update_db', default = False, help = self._('Query driver databases for newly available or updated drivers.')) parser.add_option('-l', '--list', action = 'store_true', dest = 'list', default = False, help = self._('List available drivers and their status.')) parser.add_option('--hardware-ids', action = 'store_true', dest = 'list_hwids', default = False, help = self._('List hardware identifiers from this system.')) parser.add_option('-e', '--enable', type = 'string', dest = 'enable', default = None, metavar = 'DRIVER', help = self._('Enable a driver')) parser.add_option('-d', '--disable', type = 'string', dest = 'disable', default = None, metavar = 'DRIVER', help = self._('Disable a driver')) parser.add_option('--confirm', action = 'store_true', dest = 'confirm', default = False, help = self._('Ask for confirmation for --enable/--disable')) parser.add_option('-C', '--check-composite', action = 'store_true', dest = 'check_composite', default = False, help = self._('Check if there is a graphics driver available that supports composite and offer to enable it')) parser.add_option('-m', '--mode', type = 'choice', dest = 'mode', default = 'any', choices = [ 'free', 'nonfree', 'any'], metavar = 'free|nonfree|any', help = self._('Only manage free/nonfree drivers. By default, all available drivers with any license are presented.')) parser.add_option('--dbus-server', action = 'store_true', dest = 'dbus_server', default = False, help = self._('Run as session D-BUS server.')) (opts, args) = parser.parse_args() return (opts, args) def run(self): '''Evaluate command line arguments and do the appropriate action. If no argument was specified, this starts the interactive UI. This returns the exit code of the program. ''' if self.argv_options.update_db: polkit_auth_wrapper(self.backend().update_driverdb) return 0 if self.argv_options.list: self.list() return 0 if self.argv_options.list_hwids: self.list_hwids() return 0 if self.argv_options.dbus_server: self.dbus_server() return 0 if self.argv_options.check is not None: if self.check(): return 0 return 1 self.argv_options.check is not None self.ui_init() self.have_ui = True if self.argv_options.enable: if self.set_handler_enable(self.argv_options.enable, 'enable', self.argv_options.confirm, False): return 0 return 1 self.argv_options.enable if self.argv_options.disable: if self.set_handler_enable(self.argv_options.disable, 'disable', self.argv_options.confirm, False): return 0 return 1 self.argv_options.disable if self.argv_options.check_composite: if self.check_composite(): return 0 return 1 self.argv_options.check_composite self.ui_show_main() return self.ui_main_loop() def list(self): '''Print a list of available handlers and their status to stdout.''' for h_id in self.backend().available(self.argv_options.mode): i = self.backend().handler_info(h_id) if not bool(i['free']) or self.string_free: pass if not bool(i['enabled']) or self.string_enabled: pass if not bool(i['used']) or self.string_in_use: pass print '%s - %s (%s, %s, %s)' % (h_id, i['name'], self.string_restricted, self.string_disabled, self.string_not_in_use) def list_hwids(self): '''Print a list of available handlers and their status to stdout.''' for h_id in self.backend().get_hardware(): print h_id def check(self): '''Notify the user about newly used or available drivers since last check(). Return True if any new driver is available which is not yet enabled. ''' try: (new_used, new_avail) = polkit_auth_wrapper(self.backend().new_used_available, self.argv_options.mode) except PermissionDeniedByPolicy: self.error_msg(self.string_unprivileged) return False restricted_available = False for h_id in set(new_avail): info = self.backend().handler_info(h_id) if not bool(info['announce']): new_avail.remove(h_id) continue if not bool(info['free']): restricted_available = True break continue for h_id in new_used + []: if bool(self.backend().handler_info(h_id)['free']): new_used.remove(h_id) continue notified = False if new_avail or new_used: self.ui_init() self.have_ui = True if new_avail: if restricted_available: self.ui_notification(self._('Restricted drivers available'), self._('In order to use your hardware more efficiently, you can enable drivers which are not free software.')) else: self.ui_notification(self._('New drivers available'), self._('There are new or updated drivers available for your hardware.')) notified = True elif new_used: self.ui_notification(self._('New restricted drivers in use'), self._('In order for this computer to function properly, %(os)s is using driver software that cannot be supported by %(os)s.') % { 'os': OSLib.inst.os_vendor }) notified = True if notified: self.ui_main_loop() return len(new_avail) > 0 def check_composite(self): '''Check for a composite-enabling X.org driver. If one is available and not installed, offer to install it and return True if installation succeeded. Otherwise return False. ''' h_id = self.backend().check_composite() if h_id: self.set_handler_enable(h_id, 'enable', True) return bool(self.backend().handler_info(h_id)['enabled']) self.error_msg(self._('There is no available graphics driver for your system which supports the composite extension, or the current one already supports it.')) return False def _install_progress_handler(self, phase, cur, total): if not self._install_progress_shown: self.ui_progress_start('', self._('Downloading and installing driver...'), total) self._install_progress_shown = True self.ui_progress_update(cur, total) self.ui_idle() def _remove_progress_handler(self, cur, total): if not self._install_progress_shown: self.ui_progress_start('', self._('Removing driver...'), total) self._install_progress_shown = True self.ui_progress_update(cur, total) self.ui_idle() def set_handler_enable(self, handler_id, action, confirm, gui = True): """Enable, disable, or toggle a handler. action can be 'enable', 'disable', or 'toggle'. If confirm is True, this first presents a confirmation dialog. Then a progress dialog is presented for installation/removal of the handler. If gui is True, error messags and install progress will be shown in the GUI, otherwise just printed to stderr (CLI mode). Return True if anything was changed and thus the UI needs to be refreshed. """ try: i = polkit_auth_wrapper(self.backend().handler_info, handler_id) except UnknownHandlerException: self.error_msg('%s: %s' % (self.string_unknown_driver, handler_id)) self.error_msg(self._('Use --list to see available drivers')) return False if action == 'enable': enable = True elif action == 'disable': enable = False elif action == 'toggle': enable = not bool(i['enabled']) else: raise ValueError, 'invalid action %s; allowed are enable, disable, toggle' if action == 'enable' in i: msg = i['can_change'] if gui: self.error_message(self._('Cannot change driver'), self._(msg)) else: self.error_msg(self._(msg)) return False if enable == bool(i['enabled']): return False try: if gui: try: self._install_progress_shown = False polkit_auth_wrapper(dbus_sync_call_signal_wrapper, self.backend(), 'set_enabled', { 'install_progress': self._install_progress_handler, 'remove_progress': self._remove_progress_handler }, handler_id, enable) finally: pass else: polkit_auth_wrapper(self.backend().set_enabled, handler_id, enable) except PermissionDeniedByPolicy: None if confirm else None if enable else action == 'enable' in i None if confirm else None if enable else action == 'enable' in i self.error_message('', self.string_unprivileged) return False except BackendCrashError: self._dbus_iface = None self.error_message('', '%s\n\n ubuntu-bug jockey-common\n\n%s' % (self._('Sorry, the Jockey backend crashed. Please file a bug at:'), self._('Trying to recover by restarting backend.'))) return False except SystemError: e = None self.error_message('', str(e).splitlines()[-1]) return False else: newstate = bool(self.backend().handler_info(handler_id)['enabled']) return i['enabled'] != newstate def _get_description_rationale_text(self, h_info): d = h_info.get('description', '') r = h_info.get('rationale', '') if d: d = self._(d) if r: r = self._(r) if d and r: return d.strip() + '\n\n' + r if d: return d if r: return r return '' def download_url(self, url, filename = None, data = None): '''Download an URL into a local file, and display a progress dialog. If filename is not given, a temporary file will be created. Additional POST data can be submitted for HTTP requests in the data argument (see urllib2.urlopen). Return (filename, headers) tuple, or (None, headers) if the user cancelled the download. ''' block_size = 8192 current_size = 0 try: f = urllib2.urlopen(url) except Exception: e = None self.error_message(self._('Download error'), str(e)) return (None, None) headers = f.info() if 'Content-Length' in headers: total_size = int(headers['Content-Length']) else: total_size = -1 self.ui_progress_start(self.string_download_progress_title, url, total_size) if filename: tfp = open(filename, 'wb') result_filename = filename else: (fd, result_filename) = tempfile.mkstemp() tfp = os.fdopen(fd, 'wb') try: while current_size < total_size: block = f.read(block_size) tfp.write(block) current_size += len(block) if self.ui_progress_update(current_size, total_size): if not filename: os.unlink(result_filename) result_filename = None break continue finally: tfp.close() f.close() self.ui_progress_finish() return (result_filename, headers) def error_msg(klass, msg): '''Print msg to stderr, and intercept IOErrors.''' try: print >>sys.stderr, msg except IOError: pass error_msg = classmethod(error_msg) def _call_progress_dialog(self, message, fn, *args, **kwargs): '''Call fn(*args, **kwargs) while showing a progress dialog.''' if not self.have_ui: return fn(*args, **kwargs) progress_shown = False t_fn = threading.Thread(None, fn, 'thread_call_progress_dialog', args, kwargs) t_fn.start() while True: t_fn.join(0.2) if not t_fn.isAlive(): break if not progress_shown: progress_shown = True self.ui_progress_start('', message, -1) if self.ui_progress_update(-1, -1): sys.exit(1) self.ui_idle() if progress_shown: self.ui_progress_finish() self.ui_idle() def get_displayed_handlers(self): '''Return the list of displayed handler IDs. This can either be a list of drivers which match your system, or which match a search_driver() invocation. ''' if self.current_search[0]: return self.current_search[1] return self.backend().available(self.argv_options.mode) def hwid_to_display_string(self, hwid): '''Convert a type:value hardware ID string to a human friendly text.''' try: (type, value) = hwid.split(':', 1) except ValueError: return hwid if type == 'printer_deviceid': try: import cupshelpers except ImportError: return hwid info = cupshelpers.parseDeviceID(value) return info['MFG'] + ' ' + info['MDL'] return hwid DBUS_INTERFACE_NAME = 'com.ubuntu.DeviceDriver' def dbus_server(self): '''Run session D-BUS server backend.''' dbus.mainloop.glib.DBusGMainLoop(set_as_default = True) bus = dbus.SessionBus() dbus_name = dbus.service.BusName(DBUS_BUS_NAME, bus) dbus.service.Object.__init__(self, bus, '/GUI') self.dbus_server_main_loop = gobject.MainLoop() self.dbus_server_main_loop.run() def get_dbus_client(klass): '''Return a dbus.Interface object for the server.''' obj = dbus.SessionBus().get_object(DBUS_BUS_NAME, '/GUI') return dbus.Interface(obj, AbstractUI.DBUS_INTERFACE_NAME) get_dbus_client = classmethod(get_dbus_client) def search_driver(self, hwid, sender = None, conn = None): '''Search configured driver DBs for a particular hardware component. The hardware component is described as HardwareID type and value, separated by a colon. E. g. "modalias:pci:12345" or "printer_deviceid:MFG:FooTech;MDL:X-12;CMD:GDI". This searches the enabled driver databases for a matching driver. If it finds one, it offers it to the user. This returns a pair (success, files); where \'success\' is True if a driver was found, acknowledged by the user, and installed, otherwise False; "files" is the list of files shipped by the newly installed packages (useful for e. g. printer drivers to get a list of PPDs to check). ''' self.ui_init() self.have_ui = True b = self.backend() def _srch(): if hasattr(b, '_locations'): drivers = self._dbus_iface.search_driver(hwid, timeout = 600) else: drivers = b.search_driver(hwid) self.current_search = (hwid, drivers) self._call_progress_dialog(self._('Searching driver for %s...') % self.hwid_to_display_string(hwid), _srch) result = False files = [] if self.current_search[1]: self.ui_show_main() self.ui_main_loop() for d in self.current_search[1]: info = self.backend().handler_info(d) if bool(info['enabled']) and bool(info['changed']): result = True if 'package' in info: files += self.backend().handler_files(d) 'package' in info if self.dbus_server_main_loop: self.dbus_server_main_loop.quit() return (result, files) search_driver = dbus.service.method(DBUS_INTERFACE_NAME, in_signature = 's', out_signature = 'bas', sender_keyword = 'sender', connection_keyword = 'conn')(search_driver) def convert_keybindings(self, str): """Convert keyboard accelerators to the particular UI's format. The abstract UI and drivers use the '_' prefix to mark a keyboard accelerator. A double underscore ('__') is converted to a real '_'.""" raise NotImplementedError, 'subclasses need to implement this' def ui_init(self): '''Initialize UI. This should load the GUI components, such as glade files, but not show the main window yet; that is done by ui_show_main(). ''' raise NotImplementedError, 'subclasses need to implement this' def ui_show_main(self): '''Show main window. This should set up presentation of handlers and show the main window. This must be called after ui_init(). ''' raise NotImplementedError, 'subclasses need to implement this' def ui_main_loop(self): '''Main loop for the user interface. This should return if the user wants to quit the program, and return the exit code. ''' raise NotImplementedError, 'subclasses need to implement this' def error_message(self, title, text): '''Present an error message box.''' raise NotImplementedError, 'subclasses need to implement this' def confirm_action(self, title, text, subtext = None, action = None): """Present a confirmation dialog. If action is given, it is used as button label instead of the default 'OK'. Return True if the user confirms, False otherwise. """ raise NotImplementedError, 'subclasses need to implement this' def ui_notification(self, title, text): '''Present a notification popup. This should preferably create a tray icon. Clicking on the tray icon or notification should run the GUI. ''' raise NotImplementedError, 'subclasses need to implement this' def ui_idle(self): '''Process pending UI events and return. This is called while waiting for external processes such as package installers. ''' raise NotImplementedError, 'subclasses need to implement this' def ui_progress_start(self, title, description, total): '''Create a progress dialog.''' raise NotImplementedError, 'subclasses need to implement this' def ui_progress_update(self, current, total): '''Update status of current progress dialog. current/total specify the number of steps done and total steps to do, or -1 if it cannot be determined. In this case the dialog should display an indeterminated progress bar (bouncing back and forth). This should return True to cancel, and False otherwise. ''' raise NotImplementedError, 'subclasses need to implement this' def ui_progress_finish(self): '''Close the current progress dialog.''' raise NotImplementedError, 'subclasses need to implement this'